/**
*  deferredmessage.cpp
*
*  Base class for the deferred consumer base and the
*  deferred return.
*
*  @copyright 2016
*/

/**
*  Dependencies
*/
#include "deferredmessage.h"
#include "basicheaderframe.h"
#include "bodyframe.h"

/**
*  Start namespace
*/
namespace AMQP {

	/**
	*  Process the message headers
	*
	*  @param  frame   The frame to process
	*/
	void DeferredMessage::process(BasicHeaderFrame &frame)
	{
		// store the body size
		_bodySize = frame.bodySize();

		// do we have a message?
		if (_message)
		{
			// store the body size and metadata
			_message->setBodySize(_bodySize);
			_message->set(frame.metaData());
		}

		// anybody interested in the headers?
		if (_headerCallback) _headerCallback(frame.metaData());

		// no body data expected? then we are now complete
		if (!_bodySize) complete();
	}

	/**
	*  Process the message data
	*
	*  @param  frame   The frame to process
	*/
	void DeferredMessage::process(BodyFrame &frame)
	{
		// update the bytes still to receive
		_bodySize -= frame.payloadSize();

		// anybody interested in the data?
		if (_dataCallback) _dataCallback(frame.payload(), frame.payloadSize());

		// do we have a message? then append the data
		if (_message) _message->append(frame.payload(), frame.payloadSize());

		// if all bytes were received we are now complete
		if (!_bodySize) complete();
	}
}
